package com.zhiche.lisa.bms.controller.price;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.bms.BMSApplication;
import com.zhiche.lisa.bms.dao.model.price.PriceFiRate;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.Filter;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = BMSApplication.class)
//@ActiveProfiles("test")
public class PriceControllerText {
    private MockMvc mockMvc;

    @Autowired
    private WebApplicationContext context;

    private  String token;


    @Autowired
    private Filter springSecurityFilterChain;
    @Before
    public void setUp(){
        mockMvc = MockMvcBuilders.webAppContextSetup(context).addFilter(springSecurityFilterChain).build();
//        token= uaaUtil.getTenantToken("15811132841");
        token="Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJnbXRfY3JlYXRlIjoiMTU0MDk1MzE0MDY5MSIsImFjY291bnRJZCI6MTAyLCJ1c2VyX25hbWUiOiIxNTExMDI4ODM4OUAxNjMuY29tIiwic2NvcGUiOlsiYWxsIl0sInJvbGVzIjpbeyJhdXRob3JpdHkiOiJST0xFX0FETUlOIn1dLCJ0ZW5hbnRJZCI6MSwiY29ycE5hbWUiOiLkuK3ogZTnianmtYHvvIjkuK3lm73vvInmnInpmZDlhazlj7giLCJleHAiOjE1NDA5NjAzNDAsImF1dGhvcml0aWVzIjpbIlJPTEVfQURNSU4iXSwianRpIjoiOGRjZWY1MTMtM2I1My00NzMzLThhMGItNzVhZTQ1OGE2MzI4IiwiY2xpZW50X2lkIjoiZGV2b3BzIiwidXNlcm5hbWUiOiIxNTExMDI4ODM4OUAxNjMuY29tIn0.KKU_lFQVgUdedDqBtbofG36Qx9jjJB0w6OD3B888a6o";
    }
    @Test
    public void  priceFiRateLine () throws Exception {
        Map<String,Object> condition = new HashMap<>();
        condition.put("lspName","路易通达");
        Page<PriceFiRate> page = new Page<>(1,10);
        page.setCondition(condition);
        String a= JSONObject.toJSONString(page);
        System.err.println(token);
        RequestBuilder request = MockMvcRequestBuilders.post("/price/priceFiRateLine")
                .contentType(MediaType.APPLICATION_JSON).header("Authorization",token).content(a)
                .accept(MediaType.APPLICATION_JSON);
        MvcResult mvcResult = mockMvc.perform(request).andReturn();
        int status = mvcResult.getResponse().getStatus();
        String content = mvcResult.getResponse().getContentAsString();
        System.out.println("返回结果：" + status);
        System.out.println(content);
        Assert.assertEquals(status,200);
    }
    @Test
    public void  createPriceRate() throws Exception {
        PriceFiRate priceFiRate = new PriceFiRate();
        priceFiRate.setFiRate(new BigDecimal(1.5).setScale(2));
        priceFiRate.setLspName("江西路易通达运输有限公司");
        priceFiRate.setLspId(26L);
        priceFiRate.setEffectiveDate(new Date());
        String a= JSONObject.toJSONString(priceFiRate);
        System.err.println(token);
        RequestBuilder request = MockMvcRequestBuilders.post("/priceFiRate/createPriceRate")
                .contentType(MediaType.APPLICATION_JSON).header("Authorization",token).content(a)
                .accept(MediaType.APPLICATION_JSON);
        MvcResult mvcResult = mockMvc.perform(request).andReturn();
        int status = mvcResult.getResponse().getStatus();
        String content = mvcResult.getResponse().getContentAsString();
        System.out.println("返回结果：" + status);
        System.out.println(content);
        Assert.assertEquals(status,200);
    }
    @Test
    public void selectPriceFiRate()throws Exception{
        Long lspId = 28L;
        String a= JSONObject.toJSONString(lspId);
        System.err.println(token);
        RequestBuilder request = MockMvcRequestBuilders.post("/price/queryPriceFiRateById")
                .contentType(MediaType.APPLICATION_JSON).header("Authorization",token).content(a)
                .accept(MediaType.APPLICATION_JSON);
        MvcResult mvcResult = mockMvc.perform(request).andReturn();
        int status = mvcResult.getResponse().getStatus();
        String content = mvcResult.getResponse().getContentAsString();
        System.out.println("返回结果：" + status);
        System.out.println(content);
        Assert.assertEquals(status,200);
    }

}
